﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Net;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Animation;
//using System.Windows.Shapes;

namespace EFS.RC.CCAdmin.HuntGroup
{
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{

		private EFS.Common.UI.Controls.ModalDialogHost host = null;

		#region Constructors
		/// Implicit
		public Edit()
		{
			InitializeComponent();
		}

		/// Explicit
		public Edit(
			EFS.Common.UI.Controls.ModalDialogHost host,
			System.Int32? huntgroupID)
		{
			// check
			EFS.Common.Verify.Reference(host, "host");

			InitializeComponent();

			//set listener up
		
			UtilitiesService.HuntGroups_GetDetailsCompleted += new System.EventHandler<WebProxy.HuntGroups_GetDetailsCompletedEventArgs>(HuntGroup_GetDetailsCompleted);

			//set new HuntGroups response listener
			UtilitiesService.HuntGroup_CreateCompleted +=  new System.EventHandler<WebProxy.HuntGroup_CreateCompletedEventArgs>(UtilitiesService_HuntGroup_CreateCompleted);

			//set edit response listener
			UtilitiesService.HuntGroup_UpdateCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_HuntGroup_UpdateCompleted);

			this.host = host;
			this.host.OnButtonOKClick += OnParentOKClicked;

			// set ID
			this.HuntGroupID = huntgroupID;
		}
		#endregion

		#region Properties
		/// Property ClassificationID: Get, Set
		public System.Int32? HuntGroupID { get; set;}

		/// Property IsValid: Get
		public System.Boolean? IsValid
		{
			get
			{
				return EFS.Common.Verify.String(HuntGroupName);
			}
		}

		/// Property IsValid: Get
		public System.String HuntGroupName
		{
			get
			{
				return EFS.Common.Functions.String.GetTrimmedString(textBoxHuntGroupName.Text);
			}
		}

                /// Property IsValidSip :Get
                public System.Boolean? IsValidSip
                         { 
                             get 
                              {
                                 return EFS.Common.Verify.String(HuntGroupSip);
                              } 
                        }
                public System.String HuntGroupSip
                       {
                           get
                          {

                              return EFS.Common.Functions.String.GetTrimmedString(textBoxHuntGroupSIP.Text);
                           }
                       }



		#endregion

		#region LoadData
		/// Load data
		private void LoadData()
		{
			// if edit
			if (HuntGroupID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Loading Hunt Groups");

				// call ws
				UtilitiesService.HuntGroups_GetDetailsAsync(HuntGroupID.Value);
			}
		}
		#endregion

		#region WS Callbacks
		/// Classification_GetDetailsCompleted
		private void HuntGroup_GetDetailsCompleted(object sender, WebProxy.HuntGroups_GetDetailsCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.HuntGroup huntgroup =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.HuntGroup>(e.Result.ToString());

				//bind/fill form
				textBoxHuntGroupName.Text = huntgroup.Name;
				textBoxHuntGroupSIP.Text=huntgroup.Sip;

			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve huntgroup! " + ex.Message);
			}
		}

		/// Create classification completed
		private void UtilitiesService_HuntGroup_CreateCompleted(object sender, WebProxy.HuntGroup_CreateCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to create huntgroup. " + ex.Message);
			}
		}

		/// Update classification completed
		private void UtilitiesService_HuntGroup_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to update huntgroup. " + ex.Message);
			}
		}
		#endregion

		#region Events
		/// OnParentOKClicked
		private void OnParentOKClicked(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			// test if valid
			if (IsValid.HasValue && !IsValid.Value)
			{
				return;
			}

			// create
			if (!HuntGroupID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Creating Hunt Group");

				//ws call
				UtilitiesService.HuntGroup_CreateAsync(textBoxHuntGroupName.Text,textBoxHuntGroupSIP.Text);
			}
			else
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Updating Hunt Group");

				//ws call
				UtilitiesService.HuntGroup_UpdateAsync(HuntGroupID.Value, textBoxHuntGroupName.Text,textBoxHuntGroupSIP.Text);
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion
	}
	}

